﻿//function Show() { alert("Hi !"); }
function isEmpty(testVar) { if ((testVar.length == 0) || (testVar == null)) { return true; } else { return false; } }
//function Hi(text, value) { alert('Hi: ' + text + ' : ' + value); return false; }
//function Ho(text, value) { alert('Ho: ' + text + ' : ' + value); return false; }

//Clear Item
function ClearItem(xItem) {
    //alert('BEFORE=> ' + xItem);
    xItem = xItem.replace(/\?/g, "");
    xItem = xItem.replace(/#/g, "");
    xItem = xItem.replace(/\x2a/g, "");
    xItem = xItem.replace(/$/g, "");
    xItem = xItem.replace(/{/g, "");
    xItem = xItem.replace(/}/g, "");
    xItem = xItem.replace(/</g, "");
    xItem = xItem.replace(/>/g, "");
    xItem = xItem.replace(/%/g, "");
    xItem = xItem.replace(/=/g, "");
    xItem = xItem.replace(/\^/g, "");
    xItem = xItem.replace(/_/g, "");
    xItem = xItem.replace(/\+/g, "");
    //alert('AFTER=> ' + xItem);
    return xItem;
}


/*******************************************/

function aCControl(uid, keywordid, vhfId, nvhfId, addexItem, myUL, UlClass, LiClass, LiOnDelete, backURL,
postbackois, postbackbutton, postbackoid, revpostbackbutton, ismultiple, isinsertable, cjfoiselected, cjfoideleted, lastUsedItemCount) {

    var wmt = document.getElementById(keywordid).value;
    uid = uid;
    var backCount = 0;
    var newItemCount = 0;
    var itemCount = lastUsedItemCount;
    var haveItemstoShow = false;
    var selectionState = 0;
    var delspan = "<span style='float:right; display:block;'> X </span>"

    this.FocusControl = FocusControl;
    this.ShowImage = ShowImage;
    this.HideImage = HideImage;
    this.SetSelectedValue = SetSelectedValue;
    this.addItem = addItem;
    this.ShowAddKey = ShowAddKey;
    this.HideAddKey = HideAddKey;
    this.addItemToNewItems = addItemToNewItems;
    this.deleteItemFromNewItems = deleteItemFromNewItems;
    this.delTreeItem = delTreeItem;
    this.deleteItem = deleteItem;
    this.deleteLastItem = deleteLastItem;
    this.SetUp = SetUp;
    this.SetSelectedSingleValue = SetSelectedSingleValue;
    this.addSingleItem = addSingleItem;


    function FocusControl() {
        document.getElementById(keywordid).focus();
    }

    function ShowAddKey() {
        var txt = document.getElementById(keywordid);
        var cont = document.getElementById(addexItem);
        if (txt.value.length > 0) {
            cont.style.display = 'block';
            selectionState = 1;
        } else {
            cont.style.display = 'none';
            selectionState = 0;
        }
    }

    function HideAddKey() {
        selectionState = 0;
        var cont = document.getElementById(addexItem);
        cont.style.display = 'none';
    }

    function ShowImage() {
        var el = document.getElementById(keywordid);
        el.style.backgroundImage = backURL;
        el.style.backgroundRepeat = 'no-repeat';
        el.style.backgroundPosition = 'right';
        haveItemstoShow = true;
    }

    function HideImage() {
        var el = document.getElementById(keywordid);
        el.style.backgroundImage = 'none';
        haveItemstoShow = false;
    }

    function SetSelectedValue(source, item) {
        selectionState = 0;
        var xItem = item.get_text();
        var xValue = item.get_value();

        addItem(xItem, xValue);
    }

    function addItem(xItem, xValue) {

        var str = document.getElementById(vhfId).value.toString();
        var vhfield = document.getElementById(vhfId);

        if (str.length == 0)
            vhfield.value = '#';
        if (str.indexOf('#' + xValue + '#') == -1) {
            vhfield.value += xValue + '#';
            var ul = document.getElementById(myUL);
            var li = document.createElement('LI');
            li.setAttribute("class", LiClass);
            var ccc =
 "<a href='javascript:;' onclick=\""
 + uid
 + ".deleteItem('"
 + uid
 + "_"
 + xValue
 + "'); return false; \">"
 + xItem + delspan
 + "</a>";
            li.innerHTML += ccc;
            li.id = uid + '_' + xValue;

            ul.appendChild(li);
            itemCount++;
            backCount = 0;

            if (cjfoiselected != '-1')
                eval(cjfoiselected + "()");

            if (postbackois == '1')
                document.getElementById(postbackbutton).click();
        } else {

        }


        HideAddKey();
        HideImage();
        document.getElementById(keywordid).value = '';
        setTimeout(function () { document.getElementById(keywordid).focus(); }, 50);
    }


    function delTreeItem() {
        var nvbox = document.getElementById(nvhfId);
        var vbox = document.getElementById(vhfId);
        var ids = nvbox.value.trim().split('#');
        var itemId = ids.reverse();

        for (i = 0; i < itemId.length; i++) {
            itemId[i] = itemId[i].trim();
            if (itemId[i].length > 0) {
                itemId = itemId[i]; break;
            }
        }
        var itemValues = vbox.value.split('#');
        itemValues = itemValues.reverse();

        var first;
        for (i = 0; i < itemValues.length; i++) {

            if (itemValues[i].toString().startsWith('?')) {
                var xx = itemValues[i].replace('?', "");
                first = itemValues[i]; break;
            }
        }
        deleteItemFromNewItems(itemId, first + '#');
    }



    function deleteLastItem() {
        var hbox = document.getElementById(vhfId).value;
        var arr = hbox.split('#');
        var ln = arr.length - 1;
        var ite = " ";

        for (i = 0; i < arr.length; i++) {
            if (arr[i].length != 0) ite = arr[i];
        }
        if (ite != '#') {
            if (ite.startsWith('?')) {
                delTreeItem();
            } else {
                deleteItem(uid + '_' + ite);
            }
        }
    }

    function SetUp(tbox) {
        if (tbox.onkeypress != null) { }
        else {
            tbox.onkeypress = function (e) {
                e = window.event || e;
                var keyunicode = e.charCode || e.keyCode;

                if (isinsertable == '1') {
                    if (keyunicode != 13 && keyunicode != 8) {
                        if (tbox.value.length == 1 && haveItemstoShow == false) {
                            ShowAddKey();
                        }
                    } else {
                        if (tbox.value.length == 1) {
                            HideAddKey();
                        }
                    }
                }




                if (keyunicode == 13 && selectionState == 1) {
                    //ENTER KEY PRESSED
                    if (document.getElementById(keywordid).value.length > 0) {
                        addItemToNewItems(document.getElementById(keywordid).value);
                        return false;
                    }
                    return false;
                }
                else if (keyunicode == 13) {
                    //ENTER KEY PRESSED
                    if (document.getElementById(keywordid).value.length == 0) {
                        return false;
                    }
                    return false;
                }

                var ul = document.getElementById(myUL);
                if (LiOnDelete.length > 0 && ul.lastChild != null)
                    ul.lastChild.setAttribute("class", LiClass);

                if (keyunicode == 8) {
                    //BACKSPACE PRESSED
                    if (tbox.value.length <= 1)
                        HideImage();
                    if (tbox.value.length == 0) {
                        if (itemCount > 0) {
                            backCount++;
                            if (backCount == 1 && LiOnDelete.length > 0 && ul.lastChild != null) {
                                ul.lastChild.setAttribute("class", LiOnDelete);
                            }
                            if (backCount == 2) {
                                backCount = 0;
                                deleteLastItem();
                            }
                        }
                    }
                } else { backCount = 0; }
            }
        }
    }


    function SetSelectedSingleValue(source, item) {
        selectionState = 0;
        var xItem = item.get_text();
        var xValue = item.get_value();
        addSingleItem(xItem, xValue);
    }

    function addSingleItem(xItem, xValue) {
        var str = document.getElementById(vhfId).value.toString();

        if (str.length == 0)
            document.getElementById(vhfId).value = '#';
        if (str.indexOf('#' + xValue + '#') == -1) {
            document.getElementById(vhfId).value = xValue + '#';
            var ul = document.getElementById(myUL);

            while (ul.childNodes.length > 0) {
                ul.removeChild(ul.childNodes[0]);
            }

            var li = document.createElement('LI');
            li.setAttribute("class", LiClass);
            var ccc = "<a href='javascript:;' onclick=\""
            + uid
            + ".deleteItem('"
            + uid
            + "_"
            + xValue
            + "'); return false; \" >"
            + xItem + delspan
            + "</a>";

            li.innerHTML += ccc;
            li.id = uid + '_' + xValue;
            ul.appendChild(li);
            itemCount++;
            backCount = 0;

            if (cjfoiselected != '-1')
                eval(cjfoiselected + "()");

            if (postbackois == '1')
                document.getElementById(postbackbutton).click();


        }
        else { }

        document.getElementById(keywordid).value = '';
        setTimeout(function () { document.getElementById(keywordid).focus(); }, 50);

    }



    function deleteItemFromNewItems(xItem, vItem) {

        var nvbox = document.getElementById(nvhfId);
        var vbox = document.getElementById(vhfId);
        var ids = nvbox.value.trim().split('#');

        nvbox.value = nvbox.value.replace(xItem, '');
        vbox.value = vbox.value.replace(vItem, '');

        var ul = document.getElementById(myUL);
        var li = document.getElementById(xItem);

        if (li != null) {
            ul.removeChild(li);
        }

        itemCount--;

        if (cjfoideleted != '-1')
            eval(cjfoideleted + "()");

        if (postbackoid == '1')
            document.getElementById(revpostbackbutton).click();



        setTimeout(function () { document.getElementById(keywordid).focus(); }, 50);

    }


    function deleteItem(xItem) {
        var ul = document.getElementById(myUL);
        var li = document.getElementById(xItem);
        if (li != null) { ul.removeChild(li); }

        else {
            document.getElementById(vhfId).value = '#';
            document.getElementById(nvhfId).value = '#';
            itemCount = 0;
            return;
        }

        var hbox = document.getElementById(vhfId);
        var xValue = (xItem.split('_'));
        xItem = (xValue.reverse())[0];
        hbox.value = '#' + hbox.value;
        hbox.value = hbox.value.replace(('#' + xItem + '#'), '#');

        itemCount--;

        if (cjfoideleted != '-1')
            eval(cjfoideleted + "()");

        if (postbackoid == '1')
            document.getElementById(revpostbackbutton).click();

        setTimeout(function () { document.getElementById(keywordid).focus() }, 10);
    }

    function addItemToNewItems(xItem) {
        xItem = ClearItem(xItem); HideImage();
        var str = document.getElementById(vhfId).value.toString();
        var vbox = document.getElementById(vhfId);
        var nvbox = document.getElementById(nvhfId);
        var ul = document.getElementById(myUL);

        if (ismultiple == '0') {
            vbox.value = ''; vbox.value = ''; str = ''; newItemCount = 0; itemCount = 0;
            while (ul.childNodes.length > 0) { ul.removeChild(ul.childNodes[0]); }
        }

        if (isEmpty(xItem.trim()) == true) {
            var txtbox = document.getElementById(keywordid);
            txtbox.value = '';
            setTimeout(function () { txtbox.focus(); }, 50);
            HideAddKey();
        }
        else {
            if (str.indexOf('?' + xItem.trim() + '#') == -1) {
                xItem = xItem.trim(); vbox.value += '?' + xItem.trim() + '#';
                nvbox.value += uid + '_X_' + newItemCount + '#';
                var li = document.createElement('LI');
                li.setAttribute("class", LiClass);
                var ccc = "<a href='javascript:;' onclick=\"" + uid
                + ".deleteItemFromNewItems('" + uid
                + "_X_" + newItemCount
                 + "', '?" + xItem
                 + "#'); return false; \">"
                 + xItem + delspan + "</a>";

                li.innerHTML += ccc; li.id = uid + '_X_' + newItemCount;
                ul.appendChild(li); newItemCount++;
                itemCount++; backCount = 0;

                if (cjfoiselected != '-1')
                    eval(cjfoiselected + "()");

                if (postbackois == '1')
                    document.getElementById(postbackbutton).click();

            } else { }

            HideAddKey(); HideImage();
            document.getElementById(keywordid).value = '';
            setTimeout(function () { document.getElementById(keywordid).focus(); }, 50);
        }

    }
}













/*******************************************/